package com.huaqin.customer.expansion;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.UnknownHostException;

import android.content.Intent;
import android.util.Log;

import com.huaqin.customer.SenderInterface;
import com.huaqin.salestracker.SalesTrackerService;

public class HttpProtocolSender implements SenderInterface{
	
	private static final String TAG = "HttpProtocolSender";
	private static final String PATTERN = "[*]?http://[0-9,a-z,A-Z,:,/,+,?,&,%,#,.,-,_,=]{6,80}";
	private String mResponseMessage = null;
	
	private static final int CONNECT_STATUS_OK = 200;
	private int mResponseTimeout = 0;
	private int mResponseCode = 0;

	@Override
	public boolean checkAddress(String address) {
		// TODO Auto-generated method stub
		return address.matches(PATTERN);
	}

	@Override
	public boolean send(String address, String message) {
		// TODO Auto-generated method stub

		Log.d(TAG, "send ==> address:" + address + " message:" + message);
		
		HttpURLConnection urlConnection = null;
		boolean sendResult = false;
		String cause = null;
		
		try {
			//send Data
			URL url = new URL(address); //may throws MalformedURLException
			urlConnection = (HttpURLConnection) url.openConnection(); //may throws IOException
			
			urlConnection.setDoOutput(true); 
            urlConnection.setDoInput(true);
            
            urlConnection.setRequestMethod("POST");
            
            urlConnection.setUseCaches(false);  
            urlConnection.setInstanceFollowRedirects(true);
            
            urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
            
            urlConnection.connect();
            
            OutputStream out = urlConnection.getOutputStream();
            out.write(URLEncoder.encode(message.toString(), "gb2312").getBytes());
            out.flush();
            out.close();
            
            //get Response
            mResponseCode = urlConnection.getResponseCode();
            mResponseMessage = urlConnection.getResponseMessage();
            mResponseTimeout = urlConnection.getReadTimeout();
            
            Log.v(TAG, "Send Result: ResponseCode:" + mResponseCode + " Message:[" + mResponseMessage + "] Timeout:" + mResponseTimeout);
            if(mResponseCode == CONNECT_STATUS_OK){
            	sendResult = true;
            }
            
            urlConnection.disconnect();
		}
		catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "send ==> MalformedURLException");
			cause = "MalformedURLException";
		}
		catch(UnknownHostException e){
			Log.e(TAG, "send ==> UnknownHostException");
			cause = "UnknownHostException";
		}
		catch (IOException e) {
			// TODO: handle exception
			Log.e(TAG, "send ==> IOException");
			cause = "IOException";
		}
		finally{
			Log.v(TAG, "sendBroadcast Result:" + sendResult);
			
			Intent sendIntent = new Intent(SalesTrackerService.STCMD_SEND_RESULT);
        	sendIntent.putExtra(SalesTrackerService.KEY_RESULT, sendResult);
        	//Add failed cause
        	if(false == sendResult){
        		sendIntent.putExtra(SalesTrackerService.KEY_FAILURE, cause);
        	}
			SalesTrackerService.mContext.sendBroadcast(sendIntent);
		}

		return sendResult;
	}

}
